﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using WXDba.Entity;
using WXDba.Abstract;
using System.Data.Entity;
namespace WXDba.Factory
{
    /// <summary>
    /// 消息数据操作
    /// </summary>
    internal class MsgRecordOperate : BaseOperate, IMsgRecord
    {
        #region 基础访问  
        /// <summary>  
        /// 获取表达式树  
        /// </summary>  
        /// <returns></returns>  
        public DbSet<MsgRecord> GetDbSet()
        {
            return Context.MsgRecords;
        }
        /// <summary>  
        /// 获取对象  
        /// </summary>  
        /// <param name="ID">主键ID</param>  
        /// <returns></returns>  
        public MsgRecord GetModel(int ID)
        {
            return GetDbSet().Find(ID);
        }
        /// <summary>  
        /// 添加对象  
        /// </summary>  
        /// <param name="model">对象</param>  
        /// <returns></returns>  
        public bool Add(MsgRecord model)
        {
            if (IsValided(model) == false)
                return false;
            try
            {
                GetDbSet().Add(model);
                return Save() > 0;
            }
            catch (Exception ex)
            {
                return Error_Add(ex);
            }
        }
        /// <summary>  
        /// 修改对象  
        /// </summary>  
        /// <param name="model">对象</param>  
        /// <returns></returns>  
        public bool Modify(MsgRecord model)
        {
            if (IsValided(model) == false)
                return false;
            try
            {
                return SetModify(model);
            }
            catch (Exception ex)
            {
                return Error_Modify(ex);
            }
        }
        /// <summary>  
        /// 删除对象  
        /// </summary>  
        /// <param name="model">对象</param>  
        /// <returns></returns>  
        public bool Delete(MsgRecord model)
        {
            try
            {
                GetDbSet().Remove(model);
                return Save() > 0;
            }
            catch (Exception ex)
            {
                return Error_Delete(ex);
            }
        }
        /// <summary>  
        /// 删除对象  
        /// </summary>  
        /// <param name="ID">主键ID</param>  
        /// <returns></returns>  
        public bool Delete(int ID)
        {
            MsgRecord model = GetModel(ID);
            if (model == null)
                throw new Exception(string.Format("删除对象时，获取ID={0}的对象失败", ID));
            return Delete(model);
        }
        #endregion

        #region 扩展接口
        /// <summary>
        /// 判断接收消息的MsgID的记录是否已经存在
        /// </summary>
        /// <param name="MsgID">接收消息ID</param>
        /// <returns></returns>
        public bool Exists(long MsgID)
        {
            return GetDbSet().Any(q => q.MsgID == MsgID);
        }
        #endregion

        #region 获取列表
        /// <summary>
        /// 获取交互分页消息记录
        /// </summary>
        /// <param name="info">分页信息</param>
        /// <param name="ToUserName">消息接收者</param>
        /// <param name="FromUserName">消息发送者</param>
        /// <param name="MsgType">消息类型</param>
        /// <returns></returns>
        public List<MsgRecord> GetPage(PageInfo info, string ToUserName, string FromUserName, string MsgType = null)
        {
            IQueryable<MsgRecord> query = GetDbSet();
            if (string.IsNullOrEmpty(MsgType) == false)
                query = query.Where(q => q.MsgType == MsgType);
            query = query.Where(q =>
          (q.FromUserName == FromUserName && q.ToUserName == ToUserName) ||
          (q.ToUserName == FromUserName && q.FromUserName == ToUserName));
            //排序
            info.RecordCount = query.Count();
            query = query.OrderByDescending(q => q.CreateTime);
            //分页
            return query.Skip(info.SkipCount)
                 .Take(info.PageSize)
                 .ToList();
        }
        #endregion

        #region 获取数量
        /// <summary>
        /// 获取消息记录总数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return GetDbSet().Count();
        }
        #endregion
    }
}
